﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Coolite.Ext.Web;
using FonShare.Classes.FTP;

namespace FonShare.Classes.Coolite
{
    /// <summary>
    /// Builder to create tree with Coolite Framework.
    /// </summary>
    /// <author>spesme</author>
    /// <since>26/06/2009</since>
    public static class FS_TreeBuilder
    {
        /// <summary>
        /// Builds the files tree panel.
        /// </summary>
        /// <param name="p_tree">The tree.</param>
        /// <param name="p_treeTitle">The tree title.</param>
        /// <param name="p_filesDic">The files dictionary.</param>
        /// <returns>The filled tree.</returns>
        /// <author>spesme</author>
        /// <since>26/06/2009</since>
        public static TreePanel BuildFilesTreePanel(TreePanel p_tree, string p_treeTitle, Dictionary<string, List<FS_FTPFile>> p_filesDic)
        {
            // Define Main Properties of TreePanel.
            p_tree.ID = "TreePanel1";
            p_tree.Width = System.Web.UI.WebControls.Unit.Pixel(450);
            p_tree.Height = System.Web.UI.WebControls.Unit.Pixel(500);
            p_tree.Icon = Icon.BookOpen;
            p_tree.Title = p_treeTitle;
            p_tree.AutoScroll = true;

            // Define Expand and Collapse Button and add them to the Toolbar.
            Button btnExpand = new Button();
            btnExpand.Text = "Expand All";
            btnExpand.Listeners.Click.Handler = p_tree.ClientID + ".expandAll();";
            Button btnCollapse = new Button();
            btnCollapse.Text = "Collapse All";
            btnCollapse.Listeners.Click.Handler = p_tree.ClientID + ".collapseAll();";
            Toolbar toolBar = new Toolbar();
            toolBar.ID = "Toolbar1";
            toolBar.Items.Add(btnExpand);
            toolBar.Items.Add(btnCollapse);
            p_tree.TopBar.Add(toolBar);
            StatusBar statusBar = new StatusBar();
            statusBar.AutoClear = 1000;
            p_tree.BottomBar.Add(statusBar);
            p_tree.Listeners.Click.Handler = statusBar.ClientID + ".setStatus({text: 'Node Selected: <b>' + node.text + '</b>', clear: true});";
            p_tree.Listeners.ExpandNode.Handler = statusBar.ClientID + ".setStatus({text: 'Node Expanded: <b>' + node.text + '</b>', clear: true});";
            p_tree.Listeners.ExpandNode.Delay = 30;
            p_tree.Listeners.CollapseNode.Handler = statusBar.ClientID + ".setStatus({text: 'Node Collapsed: <b>' + node.text + '</b>', clear: true});";

            TreeNode root = new TreeNode("Servers");
            root.Expanded = true;
            p_tree.Root.Add(root);

            // Create Root and Nodes.
            foreach (KeyValuePair<string, List<FS_FTPFile>> currentServer in p_filesDic)
            {
                TreeNode serverNode = new TreeNode(currentServer.Key, Icon.Server);
                root.Nodes.Add(serverNode);
                serverNode.Nodes.AddRange(GetFilesAndFolders("", currentServer.Value).AsEnumerable());
            }
            return p_tree;
        }

        /// <summary>
        /// Gets the files and folders for the current directory.
        /// </summary>
        /// <param name="p_folder">The folder.</param>
        /// <param name="p_filesList">The files list.</param>
        /// <returns>A list of TreeNode.</returns>
        /// <author>spesme</author>
        /// <since>26/06/2009</since>
        public static List<TreeNodeBase> GetFilesAndFolders(string p_folder, List<FS_FTPFile> p_filesList)
        {
            List<TreeNodeBase> result = new List<TreeNodeBase>();
            foreach (FS_FTPFile currentFile in p_filesList.Where(record => record.Directory == p_folder))
                {
                    TreeNode fileNode = new TreeNode(currentFile.Name, currentFile.IsDirectory?Icon.Folder:GetIcon(currentFile.Type));
                    result.Add(fileNode);
                    if (currentFile.IsDirectory)
                    {
                        fileNode.Nodes.AddRange(GetFilesAndFolders(currentFile.Name, p_filesList.Where(record => record.Directory == currentFile.Name).ToList()).AsEnumerable());
                    }
                }
            return result;
        }

        /// <summary>
        /// Gets the icon for the current type.
        /// </summary>
        /// <param name="p_type">The type.</param>
        /// <returns>The Icon to display.</returns>
        /// <author>spesme</author>
        /// <since>26/06/2009</since>
        public static Icon GetIcon(FS_FTPFile.FileType p_type)
        {
            switch (p_type)
            {
                case FS_FTPFile.FileType.Archive: return Icon.Compress;
                case FS_FTPFile.FileType.Book: return Icon.Book;
                case FS_FTPFile.FileType.Film: return Icon.Film;
                case FS_FTPFile.FileType.Music: return Icon.Music;
                case FS_FTPFile.FileType.Image: return Icon.Image;
                case FS_FTPFile.FileType.Unknow: return Icon.Page;
            }
            return Icon.Page;
        }
    }
}
